home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / Tutorials / Tut2 / tutorial2.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  5.1 KB  |  137 lines

  1. //-----------------------------------------------------------------------------
  2. // File: tutorial2.cpp
  3. //
  4. // Desc: DirectMusic tutorial to show how to get an object from
  5. //       an audiopath, and set the 3D position of a DirectMusic segment
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define INITGUID
  10. #include <windows.h>
  11. #include <dmusicc.h>
  12. #include <dmusici.h>
  13. #include <cguid.h>
  14.  
  15.  
  16.  
  17.  
  18. //-----------------------------------------------------------------------------
  19. // Defines, constants, and global variables
  20. //-----------------------------------------------------------------------------
  21. IDirectMusicLoader8*      g_pLoader         = NULL;
  22. IDirectMusicPerformance8* g_pPerformance    = NULL;
  23. IDirectMusicSegment8*     g_pSegment        = NULL;
  24.  
  25.  
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Name: WinMain()
  30. // Desc: Plays a single wave file using DirectMusic on the default audiopath.
  31. //-----------------------------------------------------------------------------
  32. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  33.                       INT nCmdShow )
  34. {
  35.     // Initialize COM
  36.     CoInitialize(NULL);
  37.     
  38.     // Create loader object
  39.     CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  40.                       IID_IDirectMusicLoader8, (void**)&g_pLoader );
  41.  
  42.     // Create performance object
  43.     CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
  44.                       IID_IDirectMusicPerformance8, (void**)&g_pPerformance );
  45.  
  46.     // This initializes both DirectMusic and DirectSound and 
  47.     // sets up the synthesizer. 
  48.     g_pPerformance->InitAudio( NULL, NULL, NULL, 
  49.                                DMUS_APATH_DYNAMIC_STEREO, 64,
  50.                                DMUS_AUDIOF_ALL, NULL );
  51.  
  52.     CHAR strPath[MAX_PATH];
  53.     GetWindowsDirectory( strPath, MAX_PATH );
  54.     strcat( strPath, "\\media" );
  55.  
  56.     // Tell DirectMusic where the default search path is
  57.     WCHAR wstrSearchPath[MAX_PATH];
  58.     MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
  59.                          wstrSearchPath, MAX_PATH );
  60.  
  61.     g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, 
  62.                                    wstrSearchPath, FALSE );
  63.     
  64.     // Load the segment from the file
  65.     WCHAR wstrFileName[MAX_PATH] = L"The Microsoft Sound.wav";   
  66.     if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
  67.                                                IID_IDirectMusicSegment8,
  68.                                                wstrFileName,
  69.                                                (LPVOID*) &g_pSegment ) ) )
  70.     {
  71.         MessageBox( NULL, "Media not found, sample will now quit", 
  72.                           "DirectMusic Tutorial", MB_OK );
  73.         return 0;
  74.     }
  75.  
  76.     // Download the segment's instruments to the synthesizer
  77.     g_pSegment->Download( g_pPerformance );
  78.  
  79.     // Tell DirectMusic to repeat this segment forever
  80.     g_pSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE );
  81.  
  82.     // Create an 3D audiopath with a 3d buffer.
  83.     // We can then play all segments into this buffer and directly control its
  84.     // 3D parameters.
  85.     IDirectMusicAudioPath8* p3DAudioPath = NULL;
  86.     g_pPerformance->CreateStandardAudioPath( DMUS_APATH_DYNAMIC_3D, 
  87.                                              64, TRUE, &p3DAudioPath );
  88.  
  89.     // Play segment on the 3D audiopath
  90.     g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0, 
  91.                                    0, NULL, NULL, p3DAudioPath );
  92.  
  93.     // Now DirectMusic will play in the backgroud, 
  94.     // so continue on with our task
  95.     MessageBox( NULL, "The music is now playing in center. " \
  96.                 "Click OK to pan music to left.", "DirectMusic Tutorial", MB_OK );
  97.  
  98.     // Get the IDirectSound3DBuffer8 from the 3D audiopath
  99.     IDirectSound3DBuffer8* pDSB = NULL;
  100.     p3DAudioPath->GetObjectInPath( DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, 
  101.                                  GUID_NULL, 0, IID_IDirectSound3DBuffer, 
  102.                                  (LPVOID*) &pDSB );
  103.  
  104.     // Set the position of sound a little to the left
  105.     pDSB->SetPosition( -0.1f, 0.0f, 0.0f, DS3D_IMMEDIATE );
  106.  
  107.     // Wait for input
  108.     MessageBox( NULL, "The music is now playing on the left. " \
  109.                 "Click OK to pan music to right.", "DirectMusic Tutorial", MB_OK );
  110.  
  111.     // Set the position of sound a little to the right
  112.     pDSB->SetPosition( 0.1f, 0.0f, 0.0f, DS3D_IMMEDIATE );
  113.  
  114.     // Wait for input
  115.     MessageBox( NULL, "The music is now playing on the right. " \
  116.                 "Click OK to exit.", "DirectMusic Tutorial", MB_OK );
  117.  
  118.     // Stop the music
  119.     g_pPerformance->Stop( NULL, NULL, 0, 0 );
  120.  
  121.     // Cleanup all interfaces
  122.     pDSB->Release();
  123.     p3DAudioPath->Release();
  124.     g_pLoader->Release(); 
  125.     g_pSegment->Release();
  126.  
  127.     // Close down DirectMusic after releasing the DirectSound buffers
  128.     g_pPerformance->CloseDown();
  129.     g_pPerformance->Release();
  130.     
  131.     // Close down COM
  132.     CoUninitialize();
  133.         
  134.     return 0;
  135. }
  136.  
  137.